/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2021-07-02 18:46:45
 * @LastEditors: ftt
 * @LastEditTime: 2022-03-08 09:56:43
 */
import 'package:flutter/material.dart';

class TTPopRoute<T> extends PopupRoute<T> {
  Duration duration = const Duration(microseconds: 200);
  double barrierOpacity = 0.5;
  Widget child;
  int type = 0;
  AnimationController? animationController;
  TTPopRoute({required this.child, this.barrierOpacity = 0, this.type = 1});

  @override
  Duration get transitionDuration => duration;

  @override
  Color get barrierColor => Color.fromRGBO(0, 0, 0, barrierOpacity);

  @override
  bool get barrierDismissible => true;

  @override
  String? get barrierLabel => null;

  @override
  AnimationController createAnimationController() {
    if (type == 0) {
      animationController ??=
          BottomSheet.createAnimationController(navigator!.overlay!);
      return animationController!;
    } else {
      assert(animationController == null);
      animationController =
          BottomSheet.createAnimationController(navigator!.overlay!);
      return animationController!;
    }
  }

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    Widget bottomSheet = MediaQuery.removePadding(
        context: context,
        child: InheritRouteWidget(router: this, child: child));
    return bottomSheet;
  }
}

class InheritRouteWidget extends InheritedWidget {
  final TTPopRoute router;

  const InheritRouteWidget(
      {Key? key, required this.router, required Widget child})
      : super(key: key, child: child);

  static InheritRouteWidget of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType()!;
  }

  @override
  bool updateShouldNotify(InheritRouteWidget oldWidget) {
    return oldWidget.router != router;
  }
}

class WrapLayout extends SingleChildLayoutDelegate {
  WrapLayout({
    required this.progress,
    required this.height,
  });

  final double progress;
  final double height;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    double maxHeight = height;

    return BoxConstraints(
      minWidth: constraints.maxWidth,
      maxWidth: constraints.maxWidth,
      minHeight: 0.0,
      maxHeight: maxHeight,
    );
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    double height = size.height - childSize.height * progress;
    return Offset(0.0, height);
  }

  @override
  bool shouldRelayout(WrapLayout oldDelegate) {
    return progress != oldDelegate.progress;
  }
}
